package com.calvin.study.rabbitmq.consumer;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import cloud.alibaba.study.entity.seckill.SeckillAct;
import cloud.alibaba.study.utils.GsonUtil;

@Component
public class SeckillRabbitMqConsumer {
	
	@RabbitListener(queues="dead.letter.queue")
	@RabbitHandler
	public void seckillOrderListener(@Payload String payload, Channel channel,Message message) throws Exception {
		long deliveryTag = 0 ;
		try {
			System.out.println(payload);
			SeckillAct seckillAct = GsonUtil.fromJson(payload, SeckillAct.class, GsonUtil.DATE_FORMAT);
			System.out.println(seckillAct);
			//当前消息是否重新派发
			//Boolean redelivered = message.getMessageProperties().getRedelivered();
			deliveryTag = message.getMessageProperties().getDeliveryTag();
			//完成扣减库存服务
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			//如果发生异常,则进行消费重试机制
			channel.basicNack(deliveryTag, false, false);
		}
	}

}
